package ua.edu.nuos.jeetraining2012.cms.servlet;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ua.edu.nuos.jeetraining2012.cms.dto.predefined.StandardUserStatuses;
import ua.edu.nuos.jeetraining2012.cms.entity.CourseEntity;
import ua.edu.nuos.jeetraining2012.cms.entity.StudentEntity;
import ua.edu.nuos.jeetraining2012.cms.entity.UserEntity;
import ua.edu.nuos.jeetraining2012.cms.service.Course;
import ua.edu.nuos.jeetraining2012.cms.service.Student;
import ua.edu.nuos.jeetraining2012.cms.service.Svn;
import ua.edu.nuos.jeetraining2012.cms.service.User;
import ua.edu.nuos.jeetraining2012.cms.service.exception.ServiceException;
import ua.edu.nuos.jeetraining2012.cms.service.exception.ServiceValidationException;
import ua.edu.nuos.jeetraining2012.cms.util.LocaleWrapper;
import ua.edu.nuos.jeetraining2012.cms.util.interceptor.ShiroSecurity;
import ua.edu.nuos.jeetraining2012.cms.util.mail.Mail;
import ua.edu.nuos.jeetraining2012.cms.util.mail.user.Signup;
import ua.edu.nuos.jeetraining2012.cms.util.servlet.CmsServlet;
import ua.edu.nuos.jeetraining2012.cms.util.svn.exception.SvnException;
import ua.edu.nuos.jeetraining2012.cms.util.template.layout.DefaultLayoutTemplate;
import ua.edu.nuos.jeetraining2012.cms.util.validation.EmailAddressValidator;

import javax.ejb.EJB;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;


@ShiroSecurity
@WebServlet(urlPatterns = {"/svn", "/svn/*"})
public class SvnServlet extends CmsServlet {

    /**
     * Define logger.
     */
    final static private Logger logger = LoggerFactory.getLogger(SvnServlet.class);

    /**
     * Inject BL
     */
    @EJB
    Svn svn;

    @EJB
    Course course;

    @EJB
    User user;

    // Constructors.
    public SvnServlet() {
        logger.debug("SvnServlet - invocation.");
    }

    public void createrepositoryAction(HttpServletRequest request, HttpServletResponse response) throws IOException {
        logger.debug("createrepositoryActionAction() - started.");
        String strId = request.getParameter("id");
        Integer id = Integer.valueOf(strId);
        PrintWriter out = response.getWriter();

        CourseEntity courseEntity = course.getCourseById(new Long(id));

            try {
                svn.createRepository(courseEntity);
            } catch (ServiceException e) {
                out.print(e.getMessage());
                logger.warn("SvnException - " + e.getMessage());
            }

        out.print("Create Repository finished");
        logger.debug("createrepositoryActionAction() - finished.");
    }

    public void linkAction(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String strId = request.getParameter("id");
        Integer id = Integer.valueOf(strId);

        PrintWriter out = response.getWriter();
        Subject subject = SecurityUtils.getSubject();
        UserEntity userEntity = new UserEntity();
        userEntity.setId(subject.getPrincipals().getPrimaryPrincipal().toString());

        CourseEntity courseEntity = course.getCourseById(new Long(id));
        try {
            userEntity = user.findById(userEntity);
            out.print(svn.getUserUrl(courseEntity, userEntity));
            svn.updateUserRights();
        } catch (ServiceException e) {
            logger.warn(e.getMessage());
            out.print(e.getMessage());
        } catch (ServiceValidationException e) {
            logger.warn("finish with ServiceValidationException - " + e.getMessage());
            out.print("finish with ServiceValidationException - " + e.getMessage());
        }
    }
}
